#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper. 
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq (i386,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
	BUILD_CPU = i386
	QEMU_EXE = qemu-system-x86_64
	pcbios_targets =
	qemu_targets = bios vgabios
	needed_bios_files = bios.bin vgabios.bin vgabios-cirrus.bin \
		pxe-e1000.bin pxe-ne2k_pci.bin pxe-pcnet.bin pxe-rtl8139.bin \
		extboot.bin
endif
ifeq (lpia,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
        BUILD_CPU = i386
        QEMU_EXE = qemu-system-x86_64
        pcbios_targets =
        qemu_targets = bios vgabios
        needed_bios_files = bios.bin vgabios.bin vgabios-cirrus.bin linux_boot.bin \
                pxe-e1000.bin pxe-ne2k_pci.bin pxe-pcnet.bin pxe-rtl8139.bin \
                extboot.bin
endif
ifeq (amd64,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
	BUILD_CPU = x86_64
	QEMU_EXE = qemu-system-x86_64
	pcbios_targets =
	qemu_targets = bios vgabios
	needed_bios_files = bios.bin vgabios.bin vgabios-cirrus.bin \
		pxe-e1000.bin pxe-ne2k_pci.bin pxe-pcnet.bin pxe-rtl8139.bin \
		extboot.bin
endif
ifeq (ia64,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
        BUILD_CPU = ia64
        QEMU_EXE = qemu-system-x86_64
        pcbios_targets = linux_boot.bin
        qemu_targets = bios vgabios
        needed_bios_files = bios.bin vgabios.bin vgabios-cirrus.bin linux_boot.bin \
                pxe-e1000.bin pxe-ne2k_pci.bin pxe-pcnet.bin pxe-rtl8139.bin \
                extboot.bin
endif
ifeq (powerpc,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
	BUILD_CPU = ppcemb
	QEMU_EXE = qemu-system-ppcemb
	pcbios_targets = bamboo.dtd
	qemu_targets =
	needed_bios_files = bamboo.dtd ppc_rom.bin
endif

# Removing binary blobs
unwanted_bios_files = ppc_rom.bin openbios-sparc32 video.x openbios-sparc \
	openbios-sparc64 bios.bin vgabios.bin vgabios-cirrus.bin bamboo.dtb \
	pxe-e1000.bin pxe-ne2k_pci.bin pxe-pcnet.bin pxe-rtl8139.bin

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	./configure --prefix=/usr --disable-blobs --audio-drv-list="oss alsa sdl pa" --audio-card-list="ac97 es1370 sb16 cs4231a adlib gus"


build-arch: patch config.status build-arch-stamp
build-arch-stamp:
	dh_testdir

	# Add here command to compile/build the package.
	$(MAKE) qemu user ${qemu_targets}
	for i in $(pcbios_targets); do \
		$(MAKE) -C qemu/pc-bios $$i; \
	done

	touch $@

build-indep: patch config.status build-indep-stamp

build-indep-stamp:
	dh_testdir

	# Add here command to compile/build the arch indep package.
	# It's ok not to do anything here, if you don't need to build
	#  anything for this package.
	#docbook-to-man debian/kvm.sgml > kvm.1

	touch $@

build:

#clean: unpatch config.status
clean: config.status
	dh_testdir
	#dh_testroot
	rm -f build-arch-stamp build-indep-stamp
	#rm -rf qemu/$(BUILD_CPU)-softmmu
	rm -f config.sub config.guess

	# Add here commands to clean up after the build process.
	$(MAKE) -C bios clean bios-clean
	$(MAKE) -C vgabios clean
	$(MAKE) -C qemu/pc-bios clean
	$(MAKE) -C qemu distclean
	$(MAKE) clean
	rm -f qemu/pc-bios/*.bin extboot/*.o extboot/extboot.img  extboot/extboot.bin extboot/signrom config.mak user/config.mak user/test/lib/.*.d user/test/lib/*/.*.d kernel/config.kbuild vgabios/*bin bios/acpi-dsdt.aml #vgabios/vbetables.h vgabios/vgabios.txt vgabios/vgabios.debug.txt vgabios/vgabios.cirrus.txt vgabios/vgabios.cirrus.debug.txt vgabios/*.bin vgabios/vbetables-gen vgabios/biossums


	dh_clean

ifneq "$(wildcard /usr/share/quilt/quilt.make)" ""
include /usr/share/quilt/quilt.make
endif

install: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Build & install normally
	$(MAKE) -C libkvm DESTDIR=$(CURDIR)/debian/tmp install
	$(MAKE) -C qemu DESTDIR=$(CURDIR)/debian/tmp install

	# e1000-82540em 0x8086,0x100e   Intel EtherExpressPro1000 82540EM
	if [ -e /usr/share/etherboot/e1000-82540em.zrom.gz ]; then \
		zcat /usr/share/etherboot/e1000-82540em.zrom.gz > \
		qemu/pc-bios/pxe-e1000.bin; \
	fi
	# rtl8029       0x10ec,0x8029   Realtek 8029
	if [ -e /usr/share/etherboot/rtl8029.zrom.gz ]; then \
		zcat /usr/share/etherboot/rtl8029.zrom.gz > \
		qemu/pc-bios/pxe-ne2k_pci.bin; \
	fi
	# pcnet32       0x1022,0x2000   AMD PCnet/PCI
	if [ -e /usr/share/etherboot/pcnet32.zrom.gz ]; then \
		zcat /usr/share/etherboot/pcnet32.zrom.gz > \
		qemu/pc-bios/pxe-pcnet.bin; \
	fi
	# rtl8139       0x10ec,0x8139   Realtek 8139
	if [ -e /usr/share/etherboot/rtl8139.zrom.gz ]; then \
		zcat /usr/share/etherboot/rtl8139.zrom.gz > \
		qemu/pc-bios/pxe-rtl8139.bin; \
	fi

	# Install relevant files manually
	for i in $(needed_bios_files); do \
		install -D -m 0644 qemu/pc-bios/$$i $(CURDIR)/debian/tmp/usr/share/kvm/$$i; \
	done

	# Collisions with the qemu package
	mv $(CURDIR)/debian/tmp/usr/bin/$(QEMU_EXE) $(CURDIR)/debian/tmp/usr/bin/kvm
	mv $(CURDIR)/debian/tmp/usr/bin/qemu-img $(CURDIR)/debian/tmp/usr/bin/kvm-img
	mv $(CURDIR)/debian/tmp/usr/bin/qemu-nbd $(CURDIR)/debian/tmp/usr/bin/kvm-nbd
	mv $(CURDIR)/debian/tmp/usr/share/man/man1/qemu-img.1 $(CURDIR)/debian/tmp/usr/share/man/man1/kvm-img.1
	mv $(CURDIR)/debian/tmp/usr/share/man/man1/qemu.1 $(CURDIR)/debian/tmp/usr/share/man/man1/kvm-qemu.1
	mv $(CURDIR)/debian/tmp/usr/share/man/man8/qemu-nbd.8 $(CURDIR)/debian/tmp/usr/share/man/man8/kvm-nbd.8

	install -D -m 0755 scripts/qemu-ifup $(CURDIR)/debian/tmp/etc/kvm/kvm-ifup
	install -D -m 0755 debian/reportbug-hook.sh $(CURDIR)/debian/tmp/usr/share/bug/kvm

	# Upstream does this though it is wrong for Debian, but installing it
	# properly with dh_py* would makes it clash with the binary /usr/bin/kvm
	# This seems likely to change in coming versions as upstream also
	# has /usr/bin/kvm.
	install -D -m 0755 kvm $(CURDIR)/debian/tmp/etc/kvm/utils/kvm

	# Install the userspace utilities
	install -m 0755 kvm_stat user/kvmctl user/kvmtrace user/kvmtrace_format \
		$(CURDIR)/debian/tmp/usr/bin/

	install -D -m 0755 debian/kvm-ok $(CURDIR)/debian/tmp/usr/bin/kvm-ok

	# kvm-source
	install -D -m 0755 debian/reportbug-hook.sh $(CURDIR)/debian/tmp/usr/share/bug/kvm-source
	# Copy only the driver source to the proper location
	mkdir -p debian/tmp/usr/src
	cp -r kernel  debian/tmp/usr/src/kvm-$(DEB_UPSTREAM_VERSION)
	# Copy the needed debian/ pieces to the proper location
	cp debian/dkms.conf debian/tmp/usr/src/kvm-$(DEB_UPSTREAM_VERSION)

	dh_install --sourcedir=debian/tmp --list-missing

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build-indep install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i debian/changelog.upstream
	dh_installdocs -i
#	dh_installexamples -i
#	dh_install -i
#	dh_installmenu -i
#	dh_installdebconf -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
#	dh_perl -i
#	dh_python -i
#	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build-arch install
	dh_testdir -s
	dh_testroot -s
#	dh_installdebconf -s
	dh_installdocs -s
#	dh_installexamples -s
#	dh_installmenu -s
#	dh_installlogrotate -s
#	dh_installemacsen -s
#	dh_installpam -s
#	dh_installmime -s
	dh_installinit -s --no-restart-on-upgrade --error-handler=true
#	dh_installcron -s
	dh_installman -s debian/kvm.1
	dh_installudev -s --priority=45
#	dh_installinfo -s
	dh_installchangelogs -s debian/changelog.upstream
	dh_strip -s
	dh_link -s
	dh_compress -s
	dh_fixperms -s
#	dh_makeshlibs -s
	dh_installdeb -s
#	dh_perl -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch

# get-orig-source adapted from /usr/share/gnome-pkg-tools/1/rules/sf-get-source.mk from the gnome-pkg-tools package
# copied from CDBS' buildvars
DEB_SOURCE_PACKAGE ?= $(strip $(shell egrep '^Source: ' debian/control | cut -f 2 -d ':'))
DEB_VERSION ?= $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_NOEPOCH_VERSION ?= $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION ?= $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/+[^-]*-[^-]*$$//')

# where to store the resulting .orig tarball
DEB_TARBALL_DOWNLOAD_DIR ?= ../tarballs
TARBALL_EXT ?= tar.gz

SF_PROJECT ?= $(DEB_SOURCE_PACKAGE)
SF_MODULE ?= $(DEB_SOURCE_PACKAGE)
SF_TARBALL ?= $(SF_MODULE)-$(DEB_UPSTREAM_VERSION).$(TARBALL_EXT)
SF_DOWNLOAD_URL ?= http://$$sf_mirror.dl.sourceforge.net/$(SF_PROJECT)/$(SF_TARBALL)
SF_MIRRORS ?= belnet easynews heanet internap jaist kent mesh nchc optusnet ovh puzzle superb-east superb-west surfnet switch ufpr umn
SF_DOWNLOAD_COMMAND ?= for sf_mirror in $(SF_MIRRORS); do wget -nv -T10 -t1 -O $(DEB_TARBALL_DOWNLOAD_DIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.$(TARBALL_EXT) $(SF_DOWNLOAD_URL) && break; done

get-orig-source:
	dh_testdir
	@@echo 'Source package:                      $(DEB_SOURCE_PACKAGE)'
	@@echo 'Source package:                      $(DEB_VERSION)'
	@@echo 'Source version without epoch:        $(DEB_NOEPOCH_VERSION)'
	@@echo 'Upstream version (for orig tarball): $(DEB_UPSTREAM_VERSION)'
	mkdir -p $(DEB_TARBALL_DOWNLOAD_DIR)
	$(SF_DOWNLOAD_COMMAND)
	case "$(TARBALL_EXT)" in \
	tar.bz2|tbz) \
	  bzcat $(DEB_TARBALL_DOWNLOAD_DIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.$(TARBALL_EXT) | \
	  gzip -9 >$(DEB_TARBALL_DOWNLOAD_DIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz ; \
	  rm -f $(DEB_TARBALL_DOWNLOAD_DIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.$(TARBALL_EXT) \
	  ;;\
	esac
	cd $(DEB_TARBALL_DOWNLOAD_DIR) && \
	  tar xzf $(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz
	cd $(DEB_TARBALL_DOWNLOAD_DIR) && \
#	  rm -f $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/qemu/pc-bios/openbios* && \
#	  rm -f $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/qemu/pc-bios/*.bin && \
	  rm -f $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/bios/VGABIOS* && \
	  rm -f $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/vgabios/*.bin && \
	  rm -f $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/bios/*.bin && \
	  rm -f $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/bios/*.aml
	cd $(DEB_TARBALL_DOWNLOAD_DIR) && \
	  for i in $(unwanted_bios_files); do \
	    rm -f $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/qemu/pc-bios/$$i; \
	  done
	cd $(DEB_TARBALL_DOWNLOAD_DIR) && \
	  tar c $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION) | \
	  gzip -9 >$(DEB_TARBALL_DOWNLOAD_DIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION)+dfsg.orig.tar.gz


.PHONY: build clean binary-indep binary-arch binary install binary-modules kdist kdist_configure kdist_config kdist_image kdist_clean

